package shujujiegou;

import java.util.Scanner;

public class yanghuisanjiao {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要打印的杨辉三角的行数，不建议过大以获得良好效果：");
        int rows = scanner.nextInt();
        printYangHuiTriangle(rows);
    }

    public static void printYangHuiTriangle(int rows) {
    	//外层循环控制行数，从0到rows-1
        for (int i = 0; i < rows; i++) {
            int number = 1;
            // 打印空格，使三角居中
            //空格数量随着行数的增加而减少，形成金字塔形状
            for (int j = 0; j < rows - i; j++) {
                System.out.print("  ");
            }
            //内层循环打印每行的数字
            for (int j = 0; j <= i; j++) {
                System.out.printf("%4d", number);
                number = number * (i - j) / (j + 1);
            }
            System.out.println();
        }
    }
}